package com.sunday.android.fangdaifu.bukong.fragment.tab;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.NewsTitle;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.LazyFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.member.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class NewsFragment extends LazyFragment{

    @Bind(R.id.smartLayout)
    SmartTabLayout smartLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.emptylayout)
    EmptyLayout emptylayout;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    List<NewsTitle> title = new ArrayList<>();

    private boolean isPrepared;
    private boolean mHasLoadedOnce;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_news, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        isPrepared = true;
        lazyLoad();
    }

    private void initView(){

    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        getData();
    }

    private void getData(){
        Call<ResultDO<List<NewsTitle>>> call = ApiClient.getApiAdapter().itemList();
        call.enqueue(new Callback<ResultDO<List<NewsTitle>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<NewsTitle>>> call, Response<ResultDO<List<NewsTitle>>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    title.clear();
                    title.addAll(response.body().getResult());
                    if (title.isEmpty()){
                        emptylayout.setErrorType(EmptyLayout.NODATA);
                        emptylayout.setErrorMessage("暂无数据");
                    }else {
                        emptylayout.setVisibility(View.GONE);
                        init();
                    }
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<NewsTitle>>> call, Throwable t) {
                if (isFinish) {
                    return;
                }
            }
        });
    }

    private void init() {
        initFragment();
        initPager();
    }

    private void initFragment(){
        for (int i = 0;i<title.size();i++){
            fragments.add(NewsItemFragment.newInstance(title.get(i).getId()));
        }
    }

    private void initPager(){
        Adapter adapter = new Adapter(getFragmentManager());
        viewPager.setAdapter(adapter);
        smartLayout.setViewPager(viewPager);
    }

    class Adapter extends FragmentPagerAdapter {

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title.get(position).getName();
        }
    }
}
